package com.vchello.controller.admin;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;

import com.vchello.bean.user.User;
import com.vchello.controller.BaseController;
import com.vchello.controller.constants.SubjectAttrConstants;
import com.vchello.service.user.UserService;
import com.vchello.util.ToolUtil;
import com.vchello.util.Utility;

/**
 * 
 * @author power
 *
 */
@Controller
public class LoginAdminController extends BaseController {

	@Autowired
	private UserService userService;

	@RequestMapping(value = "/admin/login", method = RequestMethod.GET)
	public String login() {
		return "admin/login";

	}

	@RequestMapping(value = "/admin/login", method = RequestMethod.POST)
	public void login(String userName, String password) {
		if (!isUserNull(userName, password)) {
			error("登录失败,用户名或密码未空");
			return;
		}

		User user = userService.adminLogin(userName, password);
		if (null == user) {
			error("登录失败,用户名或密码错误");
			return;
		}

		user.setLastLoginTime(Utility.getSimpleFormatedDayNow());
		userService.updateUser(user);
		session.setAttribute(SubjectAttrConstants.USER_AWARE_CONSTANT, user);
		success("登录成功");
	}

	private boolean isUserNull(String userName, String password) {
		if (ToolUtil.isEmpty(userName) || ToolUtil.isEmpty(password)) {
			return false;
		}
		return true;
	}

}
